package com.alex.megacj.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexProcessor {

	public static String process(String str, Pattern pattern, ReplacementHandler handler) {
		int end = 0;
		StringBuilder sb = new StringBuilder();
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			sb.append(str.substring(end, matcher.start(1)));
			end = matcher.end(1);
			String group = matcher.group(1);
			sb.append(handler.handleReplacement(group));
		}
		sb.append(str.substring(end));
		return sb.toString();
	}

}
